﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Data;

namespace NJL.WebUsers.AdminVisitor
{
    public partial class Collection : BasePage
    {
        BLL.NJL_MY_COLLECTION bll = new BLL.NJL_MY_COLLECTION();
        PagedDataSource pds;
        NJL.BLL.NJL_SYS_BASE bllSysBase = new BLL.NJL_SYS_BASE();
        NJL.Model.NJL_SYS_BASE modelSysBase;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["pager"] = "0";
                bind();
            }
        }
        void bind()
        {
            txtPage.Text = (Convert.ToInt32(ViewState["pager"]) + 1).ToString();
            pds = new PagedDataSource();
            pds.DataSource = bll.GetList("NJL_USER_ID=" + UserId).Tables[0].DefaultView;
            pds.AllowPaging = true;
            pds.PageSize = 15;
            pds.CurrentPageIndex = Convert.ToInt32(ViewState["pager"]);
            ViewState["endpage"] = pds.PageCount;
            lbtnFirst.Enabled = true;
            lbtnPrev.Enabled = true;
            lbtnNext.Enabled = true;
            lbtnEnd.Enabled = true;
            if (ViewState["pager"].ToString() == "0")
            {
                lbtnFirst.Enabled = false;
                lbtnPrev.Enabled = false;
            }
            if (Convert.ToInt32(ViewState["pager"]) == pds.PageCount - 1)
            {
                lbtnNext.Enabled = false;
                lbtnEnd.Enabled = false;
            }
            lit.Text = bll.GetList("NJL_USER_ID=" + UserId).Tables[0].Rows.Count.ToString();
            litAllPage.Text = pds.PageCount.ToString();
            litCurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();
            rptContent.DataSource = pds;
            rptContent.DataBind();
        }

        protected void lbtnFirst_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = "0";
            bind();
        }
        protected void lbtnPrev_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) - 1;
            bind();
        }
        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) + 1;
            bind();
        }
        protected void lbtnEnd_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["endpage"]) - 1;
            bind();
        }
        private static Regex RegNumber = new Regex("^[0-9]+$");
        protected void btnRir_Click(object sender, EventArgs e)
        {
            Match mc = RegNumber.Match(txtPage.Text);
            if (mc.Success && txtPage.Text != "0")
            {
                ViewState["pager"] = Convert.ToInt32(txtPage.Text) - 1;
                bind();
            }
        }
        protected void btnDel_Click1(object sender, EventArgs e)
        {
            string[] cbxid = HSelectId.Value.Split(',');
            foreach (string id in cbxid)
            {
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }
        NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        NJL.Model.NJL_COMPANY modelCompany;
        protected void rptContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal litName = (Literal)e.Item.FindControl("litName");
                Literal litDesc = (Literal)e.Item.FindControl("litDesc");
                DataRowView drv = (DataRowView)e.Item.DataItem;
                string typeId = drv["COLLECTION_TYPE"].ToString();
                string targetId = drv["TARGET_ID"].ToString();
                if (typeId == "1")
                {
                    modelCompany = bllCompany.GetModel(targetId);
                    if (modelCompany != null)
                    {
                        if (modelCompany.COMPANY_NAME!=null&&modelCompany.COMPANY_NAME.Length > 10)
                        {
                            modelCompany.COMPANY_NAME = modelCompany.COMPANY_NAME.Substring(0, 10);
                        }
                        modelSysBase = bllSysBase.GetModel(modelCompany.NJL_SYS_BASE_ID);
                        litName.Text = "(" + modelSysBase.WEBSITE_NAME + ")<a class=\"bluelink\" target=\"_blank\" href=\"http://" + modelSysBase.WEBSITE_DOMAIN+ "/shangpu/" + targetId + "-index.html\">" + modelCompany.COMPANY_NAME + "</a>";
                        if (modelCompany.COMPANY_INTRO!=null&&modelCompany.COMPANY_INTRO.Length > 10)
                        {
                            litDesc.Text = modelCompany.COMPANY_INTRO.Substring(0, 10);
                        }
                        else
                        {
                            litDesc.Text = modelCompany.COMPANY_INTRO;
                        }
                    }
                }
            }

        }

        protected void rptContent_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string id = e.CommandArgument.ToString();
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }
    }
}